//
//  WBVistorView.swift
//  SwiftWeibo
//
//  Created by Lausen on 2017/4/10.
//  Copyright © 2017年 id. All rights reserved.
//

import UIKit

class WBVistorView: UIView {

    ///注册按钮
    lazy var registerButton: UIButton = UIButton.cz_textButton(
        "注册",
        fontSize: 16,
        normalColor: UIColor.orange,
        highlightedColor: UIColor.darkGray,
        backgroundImageName: "common_button_white_disable")
    
    ///登录按钮
    lazy var loginButton: UIButton = UIButton.cz_textButton(
        "登录",
        fontSize: 16,
        normalColor: UIColor.darkGray,
        highlightedColor: UIColor.orange,
        backgroundImageName: "common_button_white_disable")
    
    /// 利用didSet来设置UI
    var vistorInfo: [String:String]?{
        
        didSet{
            
            guard let imageName = vistorInfo?["imageName"],
                let message = vistorInfo?["message"]
                else {
                    
                    return
            }
            
            tipLabel.text = message
            
            if imageName == ""{ //如果是首页的话,那就不需要重新设置图片了
                
                startAnimation()
                
                return
                
            }
            
            houseIconView.isHidden = true
            
            maskIconView.isHidden = true
            
            iconView.image = UIImage(named: imageName)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///圆圈图片视图
    fileprivate lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    /// 这个是遮罩的图片视图
    fileprivate lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    ///小房子图片视图
    fileprivate lazy var houseIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    ///标签视图
    fileprivate lazy var tipLabel: UILabel = UILabel.cz_label(
        withText: "关注一些人,回这里看看有什么惊喜,关注一些人,回这里看看有什么惊喜",
        fontSize: 14,
        color: UIColor.darkGray)
}

extension WBVistorView{
    
    
    /// 不停旋转的动画
    fileprivate func startAnimation(){
        
        //实例化动画,
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        
        //完成之后不删除,设置连续播放动画非常有用,改动画会绑定在动画图层上,循环执行
        anim.isRemovedOnCompletion = false
        //当视图销毁时,动画一起销毁
        
        iconView.layer.add(anim, forKey: nil)
    }
    
    fileprivate func setupUI(){
        
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        
        //添加子视图
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //让文字换行之后,居中显示
        tipLabel.textAlignment = .center
        
        //取消autoresizing
        for subview in subviews {
            
            subview.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //添加约束 圈圈的
        addConstraint(NSLayoutConstraint(
            item: iconView,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: self,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(
            item: iconView,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: self,
            attribute: .centerY,
            multiplier: 1.0,
            constant: 0))
        
        //添加遮罩视图的约束
        addConstraint(NSLayoutConstraint(
            item: maskIconView,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: iconView,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(
            item: maskIconView,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: iconView,
            attribute: .centerY,
            multiplier: 1.0,
            constant: 0))
        
        //小房子的约束
        addConstraint(NSLayoutConstraint(
            item: houseIconView,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: iconView,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0))
     
        addConstraint(NSLayoutConstraint(
            item: houseIconView,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: iconView,
            attribute: .centerY,
            multiplier: 1.0,
            constant: 0))
        
        //tipLabel的约束
        let margin: CGFloat = 20.0
        
        
        addConstraint(NSLayoutConstraint(
            item: tipLabel,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: iconView,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(
            item: tipLabel,
            attribute: .top,
            relatedBy: .equal,
            toItem: iconView,
            attribute: .bottom,
            multiplier: 1.0,
            constant: margin))
        
        addConstraint(NSLayoutConstraint(
            item: tipLabel,
            attribute: .width,
            relatedBy: .equal,
            toItem: nil,
            attribute: NSLayoutAttribute.notAnAttribute,
            multiplier: 1.0,
            constant: 236))
        
        //注册按钮的约束
        let length: CGFloat = 100
        
        addConstraint(NSLayoutConstraint(
            item: registerButton,
            attribute: .top,
            relatedBy: .equal,
            toItem: tipLabel,
            attribute: .bottom,
            multiplier: 1.0,
            constant: margin))
        
        addConstraint(NSLayoutConstraint(
            item: registerButton,
            attribute: .left,
            relatedBy: .equal,
            toItem: tipLabel,
            attribute: .left,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(
            item: registerButton,
            attribute: .width,
            relatedBy: .equal,
            toItem: nil,
            attribute: NSLayoutAttribute.notAnAttribute,
            multiplier: 1.0,
            constant: length))
        
        //登录按钮的约束
        addConstraint(NSLayoutConstraint(
            item: loginButton,
            attribute: .top,
            relatedBy: .equal,
            toItem: registerButton,
            attribute: .top,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(
            item: loginButton,
            attribute: .right,
            relatedBy: .equal,
            toItem: tipLabel,
            attribute: .right,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(
            item: loginButton,
            attribute: .width,
            relatedBy: .equal,
            toItem: nil,
            attribute: NSLayoutAttribute.notAnAttribute,
            multiplier: 1.0,
            constant: length))
    }
}








